/**
 * 
 */
package com.exject.android.socialphonebook.logutil;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.Contacts;
import android.text.format.DateUtils;
import android.util.Log;

import com.exject.android.socialphonebook.data.Contact;
import com.exject.android.socialphonebook.data.PhoneNumber;
import com.exject.android.socialphonebook.logutil.data.SmsLogEntry;
import com.exject.android.socialphonebook.util.RetrieveContactHelper;

/**
 * @author Benjamin Nagel <benjamin.nagel@stud.fh-dortmund.de>
 */
public class LogUtil {
    private ContentResolver contentResolver;
    
    public LogUtil(ContentResolver contentResolver) {
        this.contentResolver = contentResolver;
    }
    
    
    public Map<Date, SmsLogEntry> getSMSActivities(final long contactId) {
        Map<Date, SmsLogEntry> entries = new TreeMap<Date, SmsLogEntry>();
        Contact contact = new RetrieveContactHelper(this.contentResolver)
                .getContactById(contactId);
        
        String[] queryColumn = new String[] { "_id,thread_id,address,person,"
                + "date,protocol,read,status,type,"
                + "reply_path_present,subject,body,service_center" };
        for (int i = 0; i < contact.getPhoneNumbers().size(); i++) {
            PhoneNumber number = contact.getPhoneNumbers().get(i);
            Cursor cursor = this.contentResolver.query(Uri
                    .parse("content://sms/inbox"), queryColumn, "person == "
                    + contactId, null, null);
            final int smsIdColumn = cursor.getColumnIndex("_id");
            final int smsAddressColumn = cursor.getColumnIndex("address");
            final int smsPersonColumn = cursor.getColumnIndex("person");
            final int smsDateColumn = cursor.getColumnIndex("date");
            final int smsTypeColumn = cursor.getColumnIndex("type");
            
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    
                    int id = cursor.getInt(smsIdColumn);
                    String address = cursor.getString(smsAddressColumn);
                    String date = cursor.getString(smsDateColumn);
                    Date myDate = new Date(Long.parseLong(date));
                    int type = cursor.getInt(smsTypeColumn);
                    
                    SmsLogEntry smsLogEntry = new SmsLogEntry(id, address,
                            myDate, type, contact);
                    entries.put(myDate, smsLogEntry);
                }
            }
            cursor.close();
        }
        return entries;
    }
    
    public void getCallActivities(final long contactId) {
        Contact contact = new RetrieveContactHelper(this.contentResolver)
                .getContactById(contactId);
        
        for (int i = 0; i < contact.getPhoneNumbers().size(); i++) {
            PhoneNumber number = contact.getPhoneNumbers().get(i);
            // Querying for a cursor is like querying for any SQL-Database
            Cursor cursor = this.contentResolver.query(
                    CallLog.Calls.CONTENT_URI, null, null, null,
                    CallLog.Calls.DATE + " DESC");
            
            // Retrieve the column-indixes of phoneNumber, date and calltype
            int numberColumn = cursor.getColumnIndex(CallLog.Calls.NUMBER);
            int dateColumn = cursor.getColumnIndex(CallLog.Calls.DATE);
            // type can be: Incoming, Outgoing or Missed
            int typeColumn = cursor.getColumnIndex(CallLog.Calls.TYPE);
            int durationColumn = cursor.getColumnIndex(CallLog.Calls.DURATION);
            int newColumn = cursor.getColumnIndex(CallLog.Calls.NEW);
            
            while (cursor.moveToNext()) {
                if (cursor.getString(numberColumn).equals(
                        number.getMobileNumber())) {
                    String callerPhoneNumber = cursor.getString(numberColumn);
                    int callDate = cursor.getInt(dateColumn);
                    int callType = cursor.getInt(typeColumn);
                    long callDuration = cursor.getLong(durationColumn);
                    int callNew = cursor.getInt(newColumn);
                    
                    Drawable currentIcon = null;
                    // switch (callType) {
                    // case CallLog.Calls.INCOMING_TYPE:
                    // currentIcon = getResources().getDrawable(R.drawable.in);
                    // break;
                    // case CallLog.Calls.MISSED_TYPE:
                    // currentIcon =
                    // getResources().getDrawable(R.drawable.missed);
                    // break;
                    // case CallLog.Calls.OUTGOING_TYPE:
                    // currentIcon = getResources().getDrawable(R.drawable.out);
                    // break;
                    // }
                    // TODO: ADD icon here
                    
                    // Convert the unix-timestamp to a readable datestring
                    String dateString = DateUtils.formatElapsedTime(callDate);
                    
                    Log.i("CallLog", "Date: " + dateString);
                    Log.i("CallLog", "Phone Number: " + callerPhoneNumber);
                    Log.i("CallLog", "Type: " + callType);
                    Log.i("CallLog", "Duration: " + callDuration);
                    Log.i("CallLog", "new...: " + callNew);
                }
            }
            cursor.close();
        }
    }
    
}
